package com.sp.scmdb.serviceImpl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.DutyBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.DutyDao;
import com.sp.scmdb.dao.DutyLevelDao;
import com.sp.scmdb.entity.Duty;
import com.sp.scmdb.entity.DutyLevel;
import com.sp.scmdb.service.DutyService;
import com.sp.scmdb.service.LogService;

@Service("dutyService")
public class DutyServiceImpl implements DutyService {

	private DutyDao dutyDao;
	private DutyLevelDao dutyLevelDao;

	private LogService logs;

	@Required
	public void setLogs(LogService logs) {
		this.logs = logs;
	}
	
	public void setDutyDao(DutyDao dutyDao) {
		this.dutyDao = dutyDao;
	}
	public void setDutyLevelDao(DutyLevelDao dutyLevelDao) {
		this.dutyLevelDao = dutyLevelDao;
	}

	@Transactional(readOnly = true)
	public List<Duty> queryAll() {
		return dutyDao.queryAll();
	}

	@Transactional(readOnly = true)
	public List<DutyLevel> queryDutyLevelAll() {
		return dutyLevelDao.queryAll();
	}

	@Transactional
	public String newDuty(DutyBean t, UserBean user) {
		String result="3";
		StringBuffer Hql = new StringBuffer("from Duty where 1=1 ");
		if (StringUtils.isNotEmpty(t.getName())) {
			Hql.append("and name='").append(t.getName()).append("'");
			List<Duty> list = dutyDao.queryHql(Hql.toString());
			if (CollectionUtils.isEmpty(list)) {
				Duty duty = new Duty();
				duty.setName(t.getName());
				duty.setDutyLevelId(0);
				dutyDao.save(duty);
				logs.addLog(user, "添加 职务：\""+t.getName()+"\"成功！");
				result = CommonParamter.CHECK_SUCCESS;
			}
			else{
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	@Transactional
	public String editDuty(DutyBean bean, UserBean user) {
		String result = "2";
		// 检查角色名称是否重复
		if (StringUtils.isNotEmpty(bean.getName())) {
			StringBuffer Hql = new StringBuffer();
			Hql.append("from Duty where id != " + bean.getId()
					+ " and name = '" + bean.getName() + "'");
			if (CollectionUtils.isEmpty(dutyDao.queryHql(Hql.toString()))) {
				Duty duty = dutyDao.getById(Integer.parseInt(bean.getId()));
				// 如果名称没有重复的
				duty.setName(bean.getName());
				duty.setDutyLevelId(0);
				// 保存该职务
				dutyDao.update(duty);
				logs.addLog(user, "修改 职务：\""+bean.getName()+"\"成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	@Transactional
	public String delDuty(String[] ids, UserBean user){
		String result="1";
		Duty duty = null;
		for (int i = 0; i < ids.length; i++) {
			duty = dutyDao.getById(Integer.parseInt(ids[i]));
			if (duty != null) {
				// 删除职务信息
				logs.addLog(user, "删除 职务：\""+duty.getName()+"\"成功！");	
				dutyDao.delete(duty);			
				
				result = CommonParamter.CHECK_SUCCESS;
			}
		}
		return result;
	}

	@Transactional(readOnly = true)
	public Duty findID(String depId) {
		
		return dutyDao.getById(Integer.parseInt(depId));
	}
}
